package com.five.admin.controller;


import com.five.admin.service.ArticleTypeService;
import com.five.controller.BaseController;
import com.five.entity.ArticleType;
import com.five.entity.ArticleType;
import com.five.vo.BaseResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章分类表 前端控制器
 * </p>
 *
 * @author star
 * @since 2021-06-16
 */
@Controller
@RequestMapping("articleType")
@Api(tags = "文章列表接口")
@CrossOrigin(allowCredentials = "true",originPatterns = "*")
public class ArticleTypeController extends BaseController {
    
    @Autowired
    private ArticleTypeService articleTypeService;

    /**
     * @return文章类别列表
     */
    @ResponseBody
    @RequestMapping(value = "getArticleType",method = RequestMethod.GET)
    @ApiOperation("获取文章类型列表接口")
    public BaseResultVO getArticleType(String name, Integer page){
        List<ArticleType> list = articleTypeService.fuzzyQueryByTypename(name,page);
        int pages = ((articleTypeService.countArticleType(name)-1)/10)+1;
        Map<String,Object> resultMap = new HashMap();
        resultMap.put("pages",pages);
        resultMap.put("articleTypeList",list);
        return successResp(resultMap);
    }

    /**
     *
     * @return 返回所有文章类别
     */
    @ResponseBody
    @RequestMapping(value = "articleTypeList",method = RequestMethod.GET)
    @ApiOperation("获取文章类别列表接口")
    public BaseResultVO ArticleTypeList(){
        List<ArticleType> articleTypeList = articleTypeService.findAll();
        return successResp(articleTypeList);
    }

    @ResponseBody
    @RequestMapping(value = "insertArticleType",method = RequestMethod.POST)
    @ApiOperation("新增文章类别接口")
    public BaseResultVO insertArticleType(String name){
        articleTypeService.insertArticleType(name);
        return  successRespWithMessage("成功添加文章类别");
    }

    @ResponseBody
    @RequestMapping(value = "updateArticleType",method = RequestMethod.POST)
    @ApiOperation("更新文章类别接口")
    public BaseResultVO updateArticleType(ArticleType ArticleType){
        articleTypeService.updateArticleType(ArticleType);
        return  successRespWithMessage("成功更新文章类别");
    }

    @ResponseBody
    @RequestMapping(value = "findById",method = RequestMethod.POST)
    @ApiOperation("根据id查询文章类别")
    public BaseResultVO findById(long id){
        ArticleType articleType = articleTypeService.findById(id);
        return  successResp(articleType);
    }

    @ResponseBody
    @RequestMapping(value = "deleteById",method = RequestMethod.POST)
    @ApiOperation("根据id删除文章类别")
    public BaseResultVO deleteById(long id){
        articleTypeService.deleteArticleType(id);
        return  successRespWithMessage("成功删除文章类别");
    }

    @ResponseBody
    @RequestMapping(value = "deleteSelect",method = RequestMethod.POST)
    @ApiOperation("删除选中的文章类别")
    public BaseResultVO deleteSelect(@RequestParam(value = "ids[]") long[] ids){
        articleTypeService.deleteSelectArticleType(ids);
        return  successRespWithMessage("成功删除文章类别");
    }

    /**
     * @return文章类型列表
     */
    @ResponseBody
    @RequestMapping(value = "fuzzyQueryArticleType",method = RequestMethod.GET)
    @ApiOperation("模糊查询文章类型")
    public BaseResultVO fuzzyQueryArticleType(String name,int page){
        List<ArticleType> list = articleTypeService.fuzzyQueryByTypename(name,page);
        return successResp(list);
    }

}
